const fs = require('fs')
const dbUtil = require(process.cwd() + '/src/util/db_util')
const AppConfig = require(process.cwd() + '/src/config/app_config')
const DataReplace = require(process.cwd() + '/src/service/data_replace/data_replace')

const main = async function() {
    const DB_SYSTEM = AppConfig.DB_SYSTEM
    const DATA_FORMAT = AppConfig.DATA_FORMAT
    const OUTPUT_PATH = AppConfig.OUTPUT_PATH
    const dbSystem = new DB_SYSTEM(dbUtil, DATA_FORMAT)

    // 获取数据库系统表
    let tables = await dbSystem.getDataTables()
    let tableDatas = []
    for (let table of tables) {
        let columns = await dbSystem.getTableColumns(table.TABLE_NAME)
        columns.forEach(column => column.DATA_TYPE)
        tableDatas.push({
            TABLE_COMMENT: table.TABLE_COMMENT,
            TABLE_NAME: table.TABLE_NAME,
            columns: columns
        })
    }

    // 生成model文件
    for (let tableData of tableDatas) {
        let formatData = DataReplace.templateReplace(tableData)
        fs.writeFileSync(OUTPUT_PATH + tableData.TABLE_NAME + '.js', formatData)
    }
}
main().then(() => {
    console.log('任务结束')
    dbUtil.close()
})